<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/3/14
 * Time: 下午2:18
 */
class AuthService extends Service
{

    public static function login($account, $password){
        $user = PubAuth::getUser();
        if(!empty($user)){
            $result  = true;
            $message = PubFunc::t('您已经登录');
        }else if(empty($account) || empty($password)){
            $result  = false;
            $message = PubFunc::t('用户名或密码不能为空');
        }else{
            $modUser = new ModUser();
            $user = $modUser->searchUser($account);

            if(empty($user)){
                $result  = false;
                $message = PubFunc::t('用户不存在');
            }else{
                if($user['Password'] === md5($password)){
                	
                	$user_role_obj = new ModUserRoleMap();
                	$role_obj = new ModRole();
                	$user_role_list = $user_role_obj->findByAttributes(array('UserID'=>$user['UserID']));
                	$role_id = $user_role_list->RoleID;
                	$role_obj_list = $role_obj->findByAttributes(array('RoleID'=>$role_id));
                	$identity = $role_obj_list->identity;  //用户所属角色的分组身份
                	$ViewAllChannel = $role_obj_list->ViewAllChannel;
                	
                    $user = array(
                        'UserID'    => $user['UserID'],
                        'Account'   => $user['Account'],
                        'UserName'  => $user['UserName'],
                        'IsAdmin'   => $user['IsAdmin'],
                    	'IsViewAll' => $user['IsViewAll'],
                    	'AddBy'     => $user['AddBy'],
                    	'Identity'  => $identity,
                    	'RoleID'    => $role_id,
                    	'ViewAllChannel' => $ViewAllChannel,
                    );

                    $result = PubAuth::setUser($user);
                    $message = PubFunc::t('登录成功');
                }else{
                    $result  = false;
                    $message = PubFunc::t('密码不正确');
                }
            }
        }

        return array('state' => $result, 'msg' => $message);
    }

    // 初始化这个游戏的权限
    public static function init(){
        return true;
    }
    
    

}